<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Profile extends Frontend_Controller
{

	public function __construct()
	{
		parent::__construct();
		$this->template->set_layout('profile');


		//--PARTIALS--//
		$this->template->set_partial('header', 'header/header');
		$this->template->set_partial('footer', 'footer/footer');
		$this->load->model('gallery_model');
		$this->load->library('parser');
	}

	function get($data){

		$this->load->model('term_model');

		$id = $data['postID'];
		$postData = getPK('post',$id);


		//--MAP--//

		$long_lat = $postData['lat'].','.$postData['long'];

		$this->load->library('googlemaps');
		$config['minifyJS'] = TRUE;
		$config['map_type'] = 'ROADMAP';
		$config['trafficOverlay'] = 'TRUE';
		$config['map_height'] = '300px';
		$config['zoom'] = "16";
		$config['center'] = $long_lat;

		$config['adsense'] = TRUE;
		$config['adsensePublisherID'] = setting('adsense_pub_id');
		$config['adsenseFormat'] = 'HALF_BANNER';
		$config['adsensePosition'] = 'BOTTOM_LEFT';

		$this->googlemaps->initialize($config);

		$marker = array();
		$marker['icon'] = base_url()."assets/img/map_icons/ypmap1.png";
		$marker['position'] = $long_lat;
		$marker['infowindow_content'] = "<strong>".$postData['title']."</strong>"."</br>".$postData['address1'];
		$marker['animation'] = 'DROP';

		$this->googlemaps->add_marker($marker);
		$data['map'] = $this->googlemaps->create_map();
		//--END OF MAP --//


		//--Breadcrumb --//
		
		
		//--META DATA--//
		$this->template->title($postData['meta_title'], setting('web_title'));
		$this->template->set_metadata('keywords', $postData['meta_keyword']);
		$this->template->set_metadata('description', $postData['meta_description']);
		$this->template->set_metadata('author', setting('google_plus'));



		$data['profile'] = $postData;
		
	//	$cAT = $this->term_model->getTermValue($data['profile']['id'],'category',1);
		$cAT = getPK('category',$this->term_model->getTermValue($data['profile']['id'],'keyword',1));
		$cAT = seoUrl($cAT['name']);
		$lOC = getPK('location',$this->term_model->getTermValue($data['profile']['id'],'city',1));
		$lOC = seoUrl($lOC['name']);
		
		$this->template->set_breadcrumb($lOC,base_url().$lOC);
		$this->template->set_breadcrumb($cAT,$lOC.'/'.$cAT);
		$this->template->set_breadcrumb($data['profile']['title']);
		
		$c = $this->term_model->getTermValue($postData['id'],'city',1);
		
		if($c){
		
			$loc = getPK('location',$c);
			$data['location'] = $loc['name'];
		//	$loc = getPK('location',$c);
		
		
		
			//var_dump($loc);die();
			//die("YES");
		//	$loc = seoUrl($loc['name']);
		}else{
		
			$loc = config_item('country');
			$data['location'] = $loc;
			
		
		}
		
		


		//$data['photos'] = $this->gallery_model->getGallerydata($data['profile']['id'],'photo');
		
		$data['photos'] = $this->gallery_model->getGallery($data['profile']['id'],'photo',10);
		//var_dump($data['photos']);die();
		
		//	$data['region'] = $this->location_model->getLocation(0,9999);


		

		//--WIDGET--//
		$data['widget_contact'] = TRUE;

		$this->template->build('profile',$data);
	}

	function claim_submit()
	{
		
		$this->form_validation->set_rules('claim_name','Name','xss_clean|required');
		$this->form_validation->set_rules('claim_email','Email address','xss_clean|required|valid_email');
		$this->form_validation->set_rules('claim_email','Possition at','xss_clean|required');


		if ($this->form_validation->run() == FALSE) {
				
				
			$message = "<span class='text text-danger'>Invalied input!</span>";
				
		}
		else {

			$variable = 'claim_of_business';
				
			$email_template = $this->member_model->getTemplatebyType($variable);
			$default_mail = $this->member_model->getWebconfiguration('default_email');
			$website = $this->member_model->getWebconfiguration('web_title');
				
			$email_subject = $email_template->title;
			$email_body = $email_template->description;
			
			$data = array(
					
					'name' => $this->input->post('claim_name'),
					'email' => $this->input->post('claim_email'),
					'post_title' => $this->input->post('post_title'),
					'position_at' => $this->input->post('position_at'),
					'website_title' => $website,
					);
		
			$this->db->insert('report',$data);
			$this->load->library('parser');
			$message = $this->parser->parse_string($email_body, $data, TRUE);
			$this->member_model->send_mail($default_mail, $email_subject, $message);
			
			$message = "<span class='text text-success'>Claim report submitted...!</span>";
			
		
		}
	
			
		$output = '{ "message": "'.$message.'"}';
		echo $output;
			

	}
	
	function report_submit()
	{
	
		$this->form_validation->set_rules('report_name','Name','xss_clean|required');
		$this->form_validation->set_rules('report_email','Email','xss_clean|required|valid_email');
		$this->form_validation->set_rules('report_location','Possition at','xss_clean|required');
		$this->form_validation->set_rules('contact_no','Contact No','xss_clean|required');
		$this->form_validation->set_rules('report_type','Report Type','xss_clean|required');
		$this->form_validation->set_rules('description','Description','xss_clean|required');
	
		if ($this->form_validation->run() == FALSE) {
	
	
			$message = "<span class='text text-danger'>Invalied input!</span>";
	
		}
		else {
	
			$variable = 'report_incorrect';
				
			$email_template = $this->member_model->getTemplatebyType($variable);
			$default_mail = $this->member_model->getWebconfiguration('default_email');
			$website = $this->member_model->getWebconfiguration('web_title');
				
			$email_subject = $email_template->title;
			$email_body = $email_template->description;
			
			$data = array(
						
					'name' => $this->input->post('report_name'),
					'email' => $this->input->post('report_email'),
					'post_title' => $this->input->post('post_title'),
					'location' => $this->input->post('report_location'),
					'contact_no' => $this->input->post('contact_no'),
					'report_type' => $this->input->post('report_type'),
					'content' => $this->input->post('description'),
					'website_title' => 	$website
			);
			
			
			$this->db->insert('report',$data);
			$this->load->library('parser');
			$message = $this->parser->parse_string($email_body, $data, TRUE);
			$this->member_model->send_mail($default_mail, $email_subject, $message);
			$message = "<span class='text text-success'>Report Incorrect submitted...!</span>";
	
		}
	
			
		$output = '{ "message": "'.$message.'"}';
		echo $output;
			
	
	}
	
	function email_submit()
	{
	
		$this->form_validation->set_rules('send_email','Emails','xss_clean|required');
		
		if ($this->form_validation->run() == FALSE) {
			$message = "<span class='text text-danger'>Invalied input!</span>";
	
		}
		else {
	
			/* WAITING FOR LIVE
			 $name = $this->input->post('claim_name');
			$email = $this->input->post('claim_email');
	
			$email_subject = "Claim Report";
	
	
			*
			* $content = 'To' . $email. 'Dear Sir/Madam, '. $name . 'Thank You for Claiming Business Resport..';
			*/
			$variable = 'send_to_a_frnd';
			
			$email_template = $this->member_model->getTemplatebyType($variable);
			
			$website = $this->member_model->getWebconfiguration('web_title');

			$post_title = $this->input->post('post_title');
			
		//	$business_url = "<a href='".base_url()."$post_title' >";
			
			$business_url = $this->input->post('post_url');
			
			
			
			$email_subject = $website;
			$email_body = $email_template->description;

			
			$post_loc = $this->input->post('post_loc');
			$post_cat = $this->input->post('post_loc');
			$post_id = $this->input->post('post_id');
			$post_titl = seoUrl($this->input->post('post_title'));
				
		//	$ee = "<a href='".base_url()."$post_loc/$post_cat/$post_title-$post_id' >
		//	$post_title</a>
		//	";
			$ee = "<a href='".$business_url."' >
			$post_title</a>
			";
			
			$data = array(
					'website_title' => 	"<a href='".base_url()."'>$website</a>",
					'business_name' => "<strong><b>$ee</b></strong>",
					//'business_name' => $post_title,
					'business_address' => $this->input->post('post_address'),
					'business_keyword' => $this->input->post('post_keyword'),
					
			);
			//phprhl@gmail.com
			$email = $this->input->post('send_email');
			
			$e = explode(',', $email);
			
		//	$emails = strtr($e);
			
			//print (count($e));die();
			$ctr = count($e);
			for($i = 0; $i < $ctr; $i++){
				
				
				$EmailID = array(
						
						'email'=> $e[$i],
						
						);
				
				$this->load->library('parser');
				$message = $this->parser->parse_string($email_body,$data, TRUE);
				$this->member_model->send_mail($EmailID, $email_subject, $message);
				$message = "<span class='text text-success'>Successfully Send...!</span>";
			}
			
		
		}
	
			
		$output = '{ "message": "'.$message.'"}';
		echo $output;
			
	
	}

}